#include "Config.h"
#include <QString>
#include <QStringList>
#include "Utils.h"
#include "RegExp.h"
/*---------------------------------------------------------------------------*/
vector<Option*> options;
/*---------------------------------------------------------------------------*/
void Config::loadConfig(const char *fichier)
{
	DEBUGMSG("Config::loadConfig() : load " << fichier );
	unloadConfig();
	
	ifstream infile;
	char temp[512];
	string stemp;
	int i,j,k;
	
	
	infile.open (fichier, ifstream::in);

	if( infile.is_open() )
	{
	  	while (infile.good())
	  	{
	  		infile.getline(temp,512);
	  		stemp = temp;
 		
	  		// New way : using reg exps
	  		RegExp *reg_exp = new RegExp("*=*");
	  		
	  		if( reg_exp->match( stemp ) )
	  		{
	  			//DEBUGMSG("Match:" << reg_exp->getString(0) << "=" << reg_exp->getString(1) );
	  			
	  			
	  			
	  			QString vars = reg_exp->getString(0).c_str();
			 	QStringList vars_list = vars.split(";");
				unsigned int vars_count = vars_list.count();
				

				
		  		QString values = reg_exp->getString(1).c_str();
			 	QStringList values_list = values.split(";");
				unsigned int values_count = values_list.count();			
				
				for(int v=0; v < vars_count; v++)				
				{
					Option *opt = new Option;
	  				opt->name = vars_list[v].trimmed().toStdString();
	  				
					opt->values = new string[values_count];
					opt->count = values_count;
					
					for(int i=0;i<values_count;i++)
					{
						opt->values[i] = values_list[i].trimmed().toStdString();
					}
					
					options.push_back(opt);		
				}
				

	  		}
	  			
	  		delete reg_exp;
	  	}
	  	
	 	DEBUGMSG("Config::loadConfig() : done"); 	
  	}
  	else
  	{
  		DEBUGMSG("Config::loadConfig() : error opening file");
  		createDefaultConfig(fichier);
  	}
  	
}
/*---------------------------------------------------------------------------*/
void Config::unloadConfig()
{
	foreach(Option*, o, options) 
	{
		delete []((*o)->values);	
		delete (*o);
	}
	
	options.clear();
}
/*---------------------------------------------------------------------------*/
void Config::createDefaultConfig(const char *filename)
{
	DEBUGMSG("Config::createDefaultConfig() : " << filename); 	
	
	
	ofstream outfile;
	outfile.open (filename);

	outfile << "# gfm auto-generated default config file " << endl;
	outfile << "# -------------------------------------- " << endl;
	outfile << "# use: extension=appname;appname2;... " << endl;
	outfile << endl;
	outfile << "cpp=gedit" << endl;
	outfile << "h=gedit" << endl;
	outfile << "c=gedit" << endl;
	outfile << "txt=gedit" << endl;
	outfile << endl;
	outfile << "# gfm parameters" << endl;
	outfile << "$terminal=xterm" << endl;
	outfile << "$editor=gedit" << endl;

	outfile.close();



	Config::loadConfig(filename);
}
/*---------------------------------------------------------------------------*/
string *Config::getValues(const char *var, int &count)
{
	foreach(Option*, o, options) 
	{
		
		if( !(*o)->name.compare(var) )
		{
    		count = (*o)->count;
    		return (*o)->values;
    	}	
	}
	
	return NULL;
}
/*---------------------------------------------------------------------------*/
string Config::getValue(const char *var)
{
		
	foreach(Option*, o, options) 
	{
		if( !(*o)->name.compare(var) )
		{
    		if ( (*o)->count ) return (*o)->values[0];
    	}	
	}
	
	return "undefined";
}
/*---------------------------------------------------------------------------*/	
vector<Option*> *Config::getOptions()
{
	return &options;
}
/*---------------------------------------------------------------------------*/
